<?php

namespace app\store\service;

use app\store\model\PickPoint;

class PickService
{

    public function update($data, $id)
    {
        $pickPoint = new PickPoint();
        $address = $this->getaddress($data['lat'], $data['lng']);
        // print_r($address);exit;
        if (empty($address)) {
            return false;
        }
        $addr = '';
        $addressComponent = $address['addressComponent'];
        if (!empty($address['pois'])) {
            $pois = $address['pois'];
            $addr = $pois[0]['addr'];
        }
       
        $data['province'] = $addressComponent['province'];
        $data['city'] = $addressComponent['city'];
        $data['area'] = $addressComponent['district'];
        $data['address'] = $addressComponent['street'].$addressComponent['street_number'];
        $data['addr'] = $addr;
        $data['update_time'] = time();
        // print_r($data);exit;
        $rst = $pickPoint->where('id', $id)->save($data);
        return $rst;
    }

    public  function create($data)
    {
        $address = $this->getaddress($data['lat'], $data['lng']);
        if (empty($address)) {
            return false;
        }
        $addr = '';
        $addressComponent = $address['addressComponent'];
        if (!empty($address['pois'])) {
            $pois = $address['pois'];
            $addr = $pois[0]['addr'];
        }
       
        $data['province'] = $addressComponent['province'];
        $data['city'] = $addressComponent['city'];
        $data['area'] = $addressComponent['district'];
        $data['address'] = $addressComponent['street'].$addressComponent['street_number'];
        $data['addr'] = $addr;
        $rst = PickPoint::create($data);
        return $rst;
    }

    public static function findAll()
    {
        $pickPoint = new PickPoint();
        $list = $pickPoint->where(['store_id' => session('store_id'), 'status' => 1])->order('create_time', 'desc')->paginate(10);
        return $list;
    }

    public  function findOne($id)
    {
        $pickPoint = new PickPoint();
        $rst = $pickPoint->where(['store_id' => session('store_id'), 'status' => 1, 'id'=>$id])->find();
        return $rst;
    }

    /**
     * 根据经纬度 获取地址
     * @param $lng
     * @param $lat
     * @return mixed
     */
    public function getaddress($lng, $lat)
    {
        $url = "http://api.map.baidu.com/geocoder/v2/?ak=kBgsjMBGC4F6sxGbwcTtW2EyY7O30znk&location=" . $lng . "," . $lat . "&output=json&pois=1";
        // $url = "http://api.map.baidu.com/geocoder/v2/?ak=kBgsjMBGC4F6sxGbwcTtW2EyY7O30znk&location=" . $lat . "," . $lng . "&output=json&pois=0&location=经度,纬度&output=json&pois=0";
        // $url = "https://restapi.amap.com/v3/geocode/regeo?output=json&location=" . $lng . "," . $lat . "&key=c21f2bb337b5b1e748cf0ad0e8fce22c&radius=1000&extensions=all&batch=false&roadlevel=0";
        // $url = "https://restapi.amap.com/v3/geocode/regeo?output=json&location=".$lat.",".$lng."&key=c21f2bb337b5b1e748cf0ad0e8fce22c&radius=1000&extensions=all";
        if ($result = file_get_contents($url)) {
            $result = json_decode($result, true);
            // print_r($result);exit;
            if ($result['status'] == 0) {
                return $result['result'];
            } else {
                return false;
            }
        }
    }

}
